home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / dkbuts.zip / CATDUMP.C < prev    next >
C/C++ Source or Header  |  1991-05-16  |  5KB  |  203 lines

  1. /*
  2.  * CATDUMP.C
  3.  *
  4.  * Written by Ville Saari
  5.  *
  6.  * Copyright (c) 1991 Ferry Island Pixelboys
  7.  * All rights reserved
  8.  *
  9.  * Created: 26-Dec-90
  10.  * Updated: 31-Jan-91
  11.  *
  12.  * Updated: 07-May-91 - Aaron A. Collins - Made somewhat more portable, and
  13.  * made the program write out to a file instead of stdout.
  14.  */
  15.  
  16. #define VERSION "1.10"
  17.  
  18. #define COPYRIGHT \
  19.    "\033[33;1mCATDUMP\033[0m V" VERSION " by Ville Saari.\n"\
  20.    "Copyright (c) 1991 Ferry Island pixelboys.\n"\
  21.    "Freeware.\n"
  22.  
  23. #define USAGE \
  24.    "\n" \
  25.    "Usage: catdump <in-file1> <in-file2> [<in-file3>...] <out-file>\n"
  26.  
  27. /* #define NOANSICODES 1 */        /* uncomment if you don't want ANSI */
  28.  
  29. #include "stdio.h"
  30. #include "string.h"
  31. #include "stdlib.h"
  32.  
  33. #define BUFSIZE 32768L
  34.  
  35. FILE *outfile = NULL;
  36.  
  37. struct 
  38.    {
  39.    char *fn;
  40.    FILE *fp;
  41.    long width, height;
  42.    long startline, endline;
  43.    } infile[30];
  44.  
  45. long width, height, startline=0x7fffffffL, endline=0L;
  46.  
  47. char buf[81];
  48. char *buffer;
  49.  
  50. void error(char *text, int code)
  51.    {
  52.    if(code) fputs("CATDUMP: ", stderr);
  53.    fputs(text, stderr);
  54.    if (outfile != NULL)
  55.       {
  56.       fflush(outfile);
  57.       fclose(outfile);
  58.       }
  59.    exit(code);
  60.    }
  61.  
  62. void main(int ac, char **arg)
  63.    {
  64. #ifndef NOANSICODES
  65.    long prev;
  66. #endif
  67.    long f, ct=0, first, ok, line, linesize, bufp, buflines;
  68.  
  69.    fputs(COPYRIGHT, stderr);
  70.  
  71.    if(ac < 4 || arg[1][0] == '?' || arg[1][0] == '-')
  72.       error(USAGE, 0);
  73.  
  74.   for(f=1; f<ac; f++)
  75.       {
  76.       if(f == ac - 1)
  77.          {
  78.         if ((outfile = fopen(arg[f], "wb")) == NULL)
  79.            error("Couldn't open output file\n", 20);
  80.         continue;
  81.      }
  82.       infile[ct].fn=arg[f];
  83.       if(infile[ct].fp=fopen(infile[ct].fn, "rb"))
  84.          {
  85.          if(fread(buf, 6, 1, infile[ct].fp))
  86.             {
  87.             fseek(infile[ct].fp, 0L, 2);
  88.             infile[ct].width=(unsigned char)buf[0]|((unsigned char)buf[1]<<8);
  89.             infile[ct].height=(unsigned char)buf[2]|((unsigned char)buf[3]<<8);
  90.             infile[ct].startline=(unsigned char)buf[4]|((unsigned char)buf[5]<<8);
  91.             infile[ct].endline=infile[ct].startline+(ftell(infile[ct].fp)-4)/(infile[ct].width*3+2);
  92.  
  93.             ok=1;
  94.  
  95.             if(!ct)
  96.                {
  97.                first=f;
  98.                width=infile[ct].width;
  99.                linesize=width*3+2;
  100.                }
  101.             else
  102.                if(width!=infile[ct].width)
  103.                   {
  104.                   ok=0;
  105.                   fprintf(stderr, "CATDUMP: File '%s' incompatible with '%s'.\n",
  106.                      infile[ct].fn, arg[first]);
  107.                   fflush(stderr);
  108.                   }
  109.  
  110.             if(ok)
  111.                {
  112.                height=max((long)infile[ct].height, height);
  113.                startline=min((long)infile[ct].startline, startline);
  114.                endline=max((long)infile[ct].endline, endline);
  115.  
  116.                ct++;
  117.                }
  118.             }
  119.          else
  120.             {
  121.             infile[ct].fp=0;
  122.             fprintf(stderr, "CATDUMP: File '%s' is not a valid DKB/QRT Dump file.\n", infile[ct].fn);
  123.             fflush(stderr);
  124.             }
  125.          }
  126.       else
  127.          {
  128.          fprintf(stderr, "CATDUMP: Couldn't open file '%s'.\n", infile[ct].fn);
  129.          fflush(stderr);
  130.          }
  131.       }
  132.  
  133.    if(!ct) error("No valid input files.\n", 20);
  134.  
  135.    fprintf(outfile,"%c%c%c%c", (char)width, (char)(width>>8), (char)height, (char)(height>>8));
  136.  
  137.    buflines=BUFSIZE/linesize;
  138.    if(!(buffer=malloc((unsigned int)(linesize*buflines))))
  139.       error("Couldn't allocate memory for buffer.\n", 20);
  140.    bufp=0;
  141.  
  142. #ifndef NOANSICODES
  143.    prev=-2;
  144. #endif
  145.  
  146.    for(line=startline; line<endline; line++)
  147.       {
  148.       for(f=ct-1; f>=0 && (infile[f].startline > line || infile[f].endline <= line); f--);
  149.  
  150. #ifndef NOANSICODES
  151.       if(f!=prev)
  152.          {
  153.          if(prev!=-2) fputs("\n", stderr);
  154.          if(f==-1)
  155.             fprintf(stderr, "Lines%5d to      zeroed.\033[13D", (int)line);
  156.          else
  157.             fprintf(stderr, "Lines%5d to      copied from file '%s'.\r\033[13C",
  158.                (int)line, infile[f].fn);
  159.          prev=f;
  160.          }
  161. #endif
  162.  
  163.       if(f>=0)
  164.          {
  165.          fseek(infile[f].fp, 4L+linesize*(line-infile[f].startline), 0);
  166.          if(!fread(buffer+bufp, (unsigned int)linesize, 1, infile[f].fp) ||
  167.             (long)((unsigned char)buffer[bufp]|((unsigned char)buffer[bufp+1]<<8))!=line)
  168.             {
  169.             free(buffer);
  170.             fputs("\n", stderr);
  171.             sprintf(buf, "Error while reading input file '%s'.\n", infile[f].fn);
  172.             error(buf, 20);
  173.             }
  174.          }
  175.       else
  176.          {
  177.          memset(buffer+bufp, 0, (unsigned int)linesize);
  178.          buffer[bufp]=(char)line, buffer[bufp+1]=(char)(line>>8);
  179.          }
  180.  
  181.       if((bufp+=linesize)>=linesize*buflines || line>=endline-1)
  182.          {
  183.          if(!fwrite(buffer, (unsigned int)bufp, 1, outfile))
  184.             {
  185.             free(buffer);
  186.             fputs("\n", stderr);
  187.             error("Error while writing output file.\n", 20);
  188.             }
  189.          bufp=0;
  190.          }
  191.  
  192. #ifndef NOANSICODES
  193.       fprintf(stderr, "%5d\033[5D", (int)line);
  194.       fflush(stderr);
  195. #endif
  196.  
  197.       }
  198.    fputs("\n", stderr);
  199.  
  200.    for(f=0; f<ct; f++) fclose(infile[f].fp);
  201.    free(buffer);
  202.    }
  203.